#ifndef LOGGER_H_
#define LOGGER_H_
#include <string>
#include <fstream>
#include <time.h>

//返回时间格式化
static std::string TimeStamp()
{
	char str[9];
	time_t a = time(0);
	struct tm* b = localtime(&a);
	strftime(str, 9, "%H:%M:%S", b);
	return str;
}
//返回日期格式化
static std::string DateStamp()
{
	char str[11];
	time_t a = time(0);
	struct tm* b = localtime(&a);
	strftime(str, 11, "%Y-%m-%d", b);
	return str;
}
// ============================================================
// New Logger with a new log file and new log title
// ============================================================
class Logger
{
public:
	Logger(const std::string& p_filename,
		const std::string& p_logtitle,
		bool p_timestamp = false,
		bool p_datestamp = false)
	{
		std::fstream filetester(p_filename.c_str(), std::ios::in);
		if (filetester.is_open())
		{
			// the file exists, so just close the test file
			filetester.close();
			// open the real file and set app mode
			m_logfile.open(p_filename.c_str(), std::ios::out | std::ios::app);
		}
		else
		{
			// file doesn't exist.
			m_logfile.open(p_filename.c_str(), std::ios::out);
		}

		// print out an opening statement. Make sure it is time-and-date-stamped
		m_timestamp = true;
		m_datestamp = true;
		//m_logfile << decorator::SessionOpen();
		m_timestamp = p_timestamp;
		m_datestamp = p_datestamp;
	}

	~Logger()
	{
		m_timestamp = true;
		m_datestamp = true;
	}

	void Log(const std::string& p_entry)
	{
		std::string message;

		if (m_datestamp)
		{
			message += "[" + DateStamp() + "] ";
		}
		if (m_timestamp)
		{
			message += "[" + TimeStamp() + "] ";
		}

		message += p_entry;
		message += "\n";
		m_logfile << message;
		m_logfile.flush();
	}

protected:
	std::fstream m_logfile;
	bool m_timestamp;
	bool m_datestamp;
};

#endif